<?php
namespace app\index\controller;

use service\FeishuService;
use think\Controller;
use think\Db;

class Webhook extends Controller
{
    public function event(){
        $param = $this->request->param();
        $log_data = [];
        $insert_flag = true;
        $log_data['msg_in'] = json_encode(['request' => $this->request->request(), 'param' => $this->request->param(), 'session' => $this->request->session(), 'useragent' => $this->request->header('user-agent')],304);
        $log_data['msg_server'] = json_encode($_SERVER,304);
        if(isset($param['encrypt'])) {
            $encrypt_data = $param['encrypt'];
            $encrypt_key = FeishuService::ENCRYPTKEY;
            $base64_decode_message = base64_decode($encrypt_data);
            $iv = substr($base64_decode_message, 0, 16);
            $encrypted_event = substr($base64_decode_message, 16);
            $decrypt = openssl_decrypt($encrypted_event, 'AES-256-CBC', hash('sha256', $encrypt_key, true), OPENSSL_RAW_DATA, $iv);
            //tp_log(['param'=>$param,'encrypt'=>$encrypt_data,'decrypt' => $decrypt],"yuan");
            //print($decrypt);//首次添加链接的时候开启，后期就处理事件
            $decryptinfo = json_decode($decrypt, true);
            if (isset($decryptinfo['schema'])) {
                $log_data['schema'] = $decryptinfo['schema'];
            }
            if (isset($decryptinfo['header'])) {
                $log_data['header'] = json_encode($decryptinfo['header'], 304);
                if (isset($decryptinfo['header']['app_id'])) {
                    $log_data['app_id'] = $decryptinfo['header']['app_id'];
                }
                if (isset($decryptinfo['header']['event_type'])) {
                    $log_data['event_type'] = $decryptinfo['header']['event_type'];
                }
            }
            if (isset($decryptinfo['event'])) {
                $log_data['event'] = json_encode($decryptinfo['event'], 304);
                if (isset($log_data['event_type'])) {
                    //员工入职 //员工信息被修改 //员工离职
                    if (in_array($log_data['event_type'], ['contact.user.created_v3', 'contact.user.updated_v3', 'contact.user.deleted_v3']) && isset($decryptinfo['event']['object']['user_id']) && !empty($decryptinfo['event']['object']['user_id'])) {
                        $log_data['user_id'] = $decryptinfo['event']['object']['user_id'];
                        $log_data['open_id'] = $decryptinfo['event']['object']['open_id'];
                        $log_data['username'] = $decryptinfo['event']['object']['name'];
                        $log_data['mobile'] = str_replace('+86', '', $decryptinfo['event']['object']['mobile']);
                        FeishuService::instance()->AddEditUser($decryptinfo['event']['object']['user_id'], $log_data['event_type']);
                    } //创建部门 //修改部门信息 //删除部门
                    elseif (in_array($log_data['event_type'], ['contact.department.created_v3', 'contact.department.updated_v3', 'contact.department.deleted_v3']) && isset($decryptinfo['event']['object']['department_id']) && !empty($decryptinfo['event']['object']['department_id'])) {
                        $log_data['department_id'] = $decryptinfo['event']['object']['department_id'];
                        $log_data['title'] = $decryptinfo['event']['object']['name'];
                        $log_data['open_department_id'] = $decryptinfo['event']['object']['open_department_id'];
                        FeishuService::instance()->AddEditDepartment($decryptinfo['event']['object']['department_id'], $log_data['event_type']);
                    }

                    if(in_array($log_data['event_type'],['contact.user.updated_v3','contact.department.updated_v3'])){
                        $insert_flag = false;
                    }
                }
            }
        }
        //dump($log_data);exit;
        if(!empty($log_data) && $insert_flag){
            $log_data['c_time'] = date("Y-m-d H:i:s");
            //dump($log_data);exit;
            Db::name("webhookevent_log")->insert($log_data);
        }
    }
}